define(['text!html/airline.html', 'text!html/airlineInsert.html', 'text!html/flightInsert.html', 'css!styles/airline.css', 'js/navigationManager', 'js/serverWrapper', 'knockoutjs'], function (airlineTemplate, airlineInsertTemplate, flightInsertTemplate, _style, navigationManager, serverWrapper, ko) {
    return new function () {
        var presenter = this;

        var authenticationPresenterPromise = curl('js/authenticationPresenter');
        var destinationsPromise = serverWrapper.getAllDestinations();
        function newFlight() {
            var self = this;

            self.airlineId = ko.observable(-1);
            self.flightNumber = ko.observable();
            self.departAirport = ko.observable();
            self.arrivalAirport = ko.observable();
            self.departureDate = ko.observable();
            self.arrivalDate = ko.observable();
            self.destinations = ko.observableArray();

            $.when(destinationsPromise).then(function (desinations) {
                if (!desinations) return;
                for (var i = 0; i < desinations.length; ++i) {
                    self.destinations.push(desinations[i]);
                }
            });

            authenticationPresenterPromise.then(function (authenticationPresenter) {
                self.airlineId(authenticationPresenter.user.airline_id);
            });

            var failed = function () {
                self.isFailed(true);
            };

            self.isFailed = ko.observable(false);
            self.save = function () {
                if (self.flightNumber() == null ||
                    self.flightNumber().length == 0 ||
                    self.departAirport() == null ||
                    self.departAirport().length == 0 ||
                    self.arrivalAirport() == null ||
                    self.arrivalAirport().length == 0 ||
                    self.departureDate() == null ||
                    self.departureDate().length == 0 ||
                    self.arrivalDate() == null ||
                    self.arrivalDate().length == 0) {
                    failed();
                    return;
                }

                serverWrapper
                    .insertFlight(self.airlineId(), self.flightNumber(), self.departAirport(), self.arrivalAirport(), self.departureDate(), self.arrivalDate())
                    .success(function (result) {
                        if (result && result.status > 0) {
                            presenter.bind();
                        } else {
                            failed();
                        }
                    })
                    .error(failed);
            };
        };

        function newAirline() {
            var self = this;

            self.name = ko.observable();
            self.email = ko.observable();
            self.password = ko.observable();
            self.isFailed = ko.observable(false);
            var failed = function () {
                self.isFailed(true);
            };

            self.save = function () {
                if (self.name() == null ||
                    self.name().length == 0 ||
                    self.email() == null ||
                    self.email().length == 0 ||
                    self.password() == null ||
                    self.password().length == 0) {
                    failed();
                    return;
                }

                serverWrapper
                    .insertAirline(self.name(), self.email(), self.password())
                    .success(function (result) {
                        if (result && result.status > 0) {
                            curl('js/adminPresenter', function (adminPresenter) {
                                adminPresenter.bind();
                            });
                        } else {
                            failed();
                        }
                    })
                    .error(failed);
            };
        };

        function flightViewModel(flight) {
            var self = this;

            self.flightId = ko.observable(flight.flight_id);
            self.flightNumber = ko.observable(flight.flight_number);
            self.departAirport = ko.observable(flight.depart_airport);
            self.arrivalAirport = ko.observable(flight.arrival_airport);
            self.departDate = ko.observable(flight.depart_date);
            self.isExpanded = ko.observable(false);
            self.onDelete = $.Callbacks();

            self.flightClick = function () {
                self.isExpanded(!self.isExpanded());
            };

            self.deleteClick = function () {
                serverWrapper
                    .deleteFlight(self.flightId)
                    .success(function (result) {
                        if (!result || result.status == 0) return;
                        self.onDelete.fire(self);
                    });
            };
        };

        function airlinesViewModel(airline) {
            var self = this;

            self.airlineId = ko.observable(-1);
            self.name = ko.observable(null);
            authenticationPresenterPromise.then(function (authenticationPresenter) {
                self.airlineId(authenticationPresenter.user.airline_id);
                self.name(authenticationPresenter.user.name);
            });

            self.flights = ko.observableArray();

            self.removeFlight = function (flight) {
                self.flights.remove(flight);
            };

            self.insertClick = function () {
                navigationManager.load('insertFlight', flightInsertTemplate, new newFlight());
            };

            self.logoff = function () {
                authenticationPresenterPromise.then(function (authenticationPresenter) {
                    authenticationPresenter.logoff();
                });
            };

            serverWrapper
                .getAllFlights(self.airlineId())
                .success(function (result) {
                    if (!result || result.length == 0) return;
                    result.forEach(function (flight) {
                        var flightModel = new flightViewModel(flight);
                        flightModel.onDelete.add(self.removeFlight);
                        self.flights.push(flightModel);
                    });
                });
        };

        presenter.insert = function () {
            navigationManager.load('insertAirline', airlineInsertTemplate, new newAirline());
        };

        presenter.bind = function () {
            navigationManager.load('airline', airlineTemplate, new airlinesViewModel());
        };
    };
});